﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreSystem : MonoSingleton<ScoreSystem>
{
    public Text scoreTxt;
    private int mTotalScore;
    private ArrayList mDScoreList;
    private int mCurDScore = 0;
    
    // Start is called before the first frame update
    void Start()
    {
        mDScoreList = new ArrayList();
        mTotalScore = PlayerPrefs.GetInt("TotalScore",0);
        mTotalScore = 0;
    }

    // Update is called once per frame
    void Update()
    {
        // ScoreSystem.Instance 
        if (scoreTxt != null)
        {
            if(mCurDScore > 0) {
                UpdateScore();
            }
            else
            {
                if(mDScoreList.Count > 0)
                {
                    DScore dScore = (DScore)mDScoreList[0];
                    if (!dScore.bStep)
                    {
                        mTotalScore += dScore.dScore;
                        mCurDScore = 0;
                        UpdateScore();
                    }
                    else
                    {
                        mCurDScore = dScore.dScore;
                        mDScoreList.RemoveAt(0);
                    }
                }
            }
            
        }
    }
    private void UpdateScore()
    {
        if(mCurDScore > 0)
        {
            mTotalScore++;
            mCurDScore--;
        }else if(mCurDScore < 0)
        {
            mTotalScore--;
            mCurDScore++;
        }
        scoreTxt.text = "" + mTotalScore;
    }
    
    /// <summary>
    /// 增加分数
    ///
    /// </summary>
    /// <param name="dScore">增加多少分,可以是负数</param>
    /// <param name="bStep">是否动画步进增加</param>
    public void AddScore(int dScore,bool bStep)
    {
        mDScoreList.Add(new DScore(dScore,bStep));
        int temp = PlayerPrefs.GetInt("TotalScore", 0);
        PlayerPrefs.SetInt("TotalScore", temp + dScore);
    }

    public void TestAddScore()
    {
        AddScore(10, true);
    }



    private class DScore
    {
        public DScore(int pDScore, bool pBStep)
        {
            this.dScore = pDScore;
            this.bStep = pBStep;
        }
        public int dScore;
        public bool bStep;
    }
}
